<?php
namespace Acme\SecurityBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;

use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use JMS\SecurityExtraBundle\Annotation\Secure;

class SecurityController extends Controller {
  function loginAction() {
  	$request = $this->getRequest();
  	$session = $this->get('session');
  	
  	// get the login error if there is one
  	if($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR))
  	{
  		$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
  	}else{
  		$error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
  	}
  	
  	return $this->render('AcmeSecurityBundle:Security:login.html.twig', array(
  	   'last_username' => $session->get(SecurityContext::LAST_USERNAME),
  	   'error' => $error
  	));
  }
  
  /**
   * @Secure(roles="ROLE_ADMIN")
   *
   * @param unknown_type $name
   */
  function helloAction($name)
  {
  	if(false === $this->get('security.context')->isGranted('ROLE_ADMIN'))
  	{
  		throw new AccessDeniedException(); 
  	}
  }
  
}
